package com.objetdirect.gwt.umldrawer.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.widgets.MessageBox;
import com.objetdirect.gwt.umldrawer.shared.services.DeveloperService;
import com.objetdirect.gwt.umldrawer.shared.services.DeveloperServiceAsync;

public class SignUpPage implements PageContent {

	private VerticalPanel mainPanel = new VerticalPanel();
	private VerticalPanel signUpContent = new VerticalPanel();
	private HorizontalPanel signUpWithImage = new HorizontalPanel();
	private VerticalPanel signUpPanel = new VerticalPanel();
	private Image umlImage = new Image();
	private Label signUpLabel = new Label();
	private Label firstNameLabel = new Label();
	private Label lastNameLabel = new Label();
	private Label emailLabel = new Label();
	private Label passwordLabel = new Label();
	private Label confirmPasswordLabel = new Label();
	private TextBox firstName = new TextBox();
	private TextBox lastName = new TextBox();
	private TextBox email = new TextBox();
	private PasswordTextBox password = new PasswordTextBox();
	private PasswordTextBox confirmPassword = new PasswordTextBox();
	private Label checkFirstNameLabel = new Label();
	private Image signUpButton = new Image("images/signUp.png");
	private CheckBox acceptPrivacy = new CheckBox();
	private Image header = new Image();
	private Image footer = new Image();

	private DeveloperServiceAsync developerService = GWT
			.create(DeveloperService.class);

	@Override
	public void setPageContent() {

		header.setUrl("images/header.png");
		header.setWidth("1201px");
		header.setStyleName("headerImage");

		signUpLabel.setText("Sign Up");
		signUpLabel.setStyleName("pagesHeader");

		firstNameLabel.setText("First Name :");
		firstNameLabel.setStyleName("labels");

		checkFirstNameLabel.setText("You Must Insert Your First Name");
		checkFirstNameLabel.setStyleName("textBoxCheck");
		checkFirstNameLabel.setVisible(false);

		lastNameLabel.setText("Last Name :");
		lastNameLabel.setStyleName("labels");

		emailLabel.setText("E-mail :");
		emailLabel.setStyleName("labels");

		passwordLabel.setText("Password :");
		passwordLabel.setStyleName("labels");

		confirmPasswordLabel.setText("Confirm Password :");
		confirmPasswordLabel.setStyleName("labels");

		firstName.setStyleName("textBox");
		lastName.setStyleName("textBox");
		email.setStyleName("textBox");
		password.setStyleName("textBox");
		confirmPassword.setStyleName("textBox");

		signUpButton.setStyleName("ProDesUMLButton");
		acceptPrivacy
				.setText(" Yes, I have read and accepted the ProDesUML Terms of Use and "
						+ "the Privacy Statement.");
		acceptPrivacy.setStyleName("labels");

		footer.setUrl("images/footer.png");
		footer.setWidth("1201px");

		signUpContent.add(new HeaderButtons().getHeaderButtons());
		signUpContent.add(signUpLabel);
		signUpContent.add(checkFirstNameLabel);
		signUpContent.add(firstNameLabel);
		signUpContent.add(firstName);
		signUpContent.add(lastNameLabel);
		signUpContent.add(lastName);
		signUpContent.add(emailLabel);
		signUpContent.add(email);
		signUpContent.add(passwordLabel);
		signUpContent.add(password);
		signUpContent.add(confirmPasswordLabel);
		signUpContent.add(confirmPassword);
		signUpContent.add(new HTML("<br>"));
		signUpContent.add(acceptPrivacy);
		signUpContent.add(signUpButton);

		signUpButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				validateSignUp();
			}
		});

		signUpButton.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				signUpButton.setUrl("images/signUp_.png");
			}
		});

		signUpButton.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				signUpButton.setUrl("images/signUp.png");
			}
		});

		umlImage.setUrl("images/image.jpg");
		umlImage.setStyleName("homeLeftImage");

		signUpWithImage.add(umlImage);
		signUpWithImage.add(signUpContent);

		signUpPanel.add(signUpWithImage);
		signUpPanel.add(new PrivacyBar().getprivacyBar());
		signUpPanel.setStyleName("border");

		mainPanel.add(header);
		mainPanel.add(signUpPanel);
		mainPanel.add(footer);
	}

	@Override
	public VerticalPanel getPageContent() {
		setPageContent();
		return mainPanel;
	}

	@SuppressWarnings("deprecation")
	public void validateSignUp() {
		String errorMsg = "";
		if (firstName.getText().equals("")) {
			errorMsg += "-	You Must Enter Your First Name.<br>";
		}
		if (lastName.getText().equals("")) {
			errorMsg += "-	You Must Enter Your Last Name.<br>";
		}
		if (email.getText().equals("")) {
			errorMsg += "-	You Must Enter Your E-Mail.<br>";
		}
		if (email.getText().endsWith(".")) {
			errorMsg += "-	Invalid E-mail.<br>";
		}
		if (password.getText().equals("")) {
			errorMsg += "-	You Must Enter Your Password.<br>";
		}
		if (confirmPassword.getText().equals("")) {
			errorMsg += "-	You Must Re-Enter Your Password.<br>";
		}
		if (password.getText().length() > 0
				&& confirmPassword.getText().length() > 0
				&& !(password.getText().equals(confirmPassword.getText()))) {
			errorMsg += "-	Your password doesn't match.<br>";
		}
		if (acceptPrivacy.isChecked() == false) {
			errorMsg += "-	You Must Accept Our Term Privacy.<br>";
		}

		if (errorMsg.equals("")) {

			developerService.saveDeveloper(email.getText(),
					firstName.getText(), lastName.getText(),
					password.getText(), 1, new AsyncCallback<Void>() {

						@Override
						public void onSuccess(Void result) {
							History.newItem("WorkSpace");
						}

						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("Server Error", "Error Connection");
						}
					});
		} else {
			MessageBox.setMinWidth(300);
			MessageBox.alert("Validation Error", errorMsg);
		}
	}
}
